package cn.iocoder.yudao.module.system.controller.admin.camera;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.camera.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.camera.CameraDO;
import cn.iocoder.yudao.module.system.convert.camera.CameraConvert;
import cn.iocoder.yudao.module.system.service.camera.CameraService;

@Api(tags = "管理后台 - 设备列表")
@RestController
@RequestMapping("/system/camera")
@Validated
public class CameraController {

    @Resource
    private CameraService cameraService;

    @PostMapping("/create")
    @ApiOperation("创建设备列表")
    @PreAuthorize("@ss.hasPermission('system:camera:create')")
    public CommonResult<Long> createCamera(@Valid @RequestBody CameraCreateReqVO createReqVO) {
        return success(cameraService.createCamera(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新设备列表")
    @PreAuthorize("@ss.hasPermission('system:camera:update')")
    public CommonResult<Boolean> updateCamera(@Valid @RequestBody CameraUpdateReqVO updateReqVO) {
        cameraService.updateCamera(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除设备列表")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:camera:delete')")
    public CommonResult<Boolean> deleteCamera(@RequestParam("id") Long id) {
        cameraService.deleteCamera(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得设备列表")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:camera:query')")
    public CommonResult<CameraRespVO> getCamera(@RequestParam("id") Long id) {
        CameraDO camera = cameraService.getCamera(id);
        return success(CameraConvert.INSTANCE.convert(camera));
    }

    @GetMapping("/list")
    @ApiOperation("获得设备列表列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:camera:query')")
    public CommonResult<List<CameraRespVO>> getCameraList(@RequestParam("ids") Collection<Long> ids) {
        List<CameraDO> list = cameraService.getCameraList(ids);
        return success(CameraConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得设备列表分页")
    @PreAuthorize("@ss.hasPermission('system:camera:query')")
    public CommonResult<PageResult<CameraRespVO>> getCameraPage(@Valid CameraPageReqVO pageVO) {
        PageResult<CameraDO> pageResult = cameraService.getCameraPage(pageVO);
        return success(CameraConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出设备列表 Excel")
    @PreAuthorize("@ss.hasPermission('system:camera:export')")
    @OperateLog(type = EXPORT)
    public void exportCameraExcel(@Valid CameraExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CameraDO> list = cameraService.getCameraList(exportReqVO);
        // 导出 Excel
        List<CameraExcelVO> datas = CameraConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备列表.xls", "数据", CameraExcelVO.class, datas);
    }

}
